package com.wanchuang.shengfubao.adapter;


import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.wanchuang.shengfubao.R;
import com.wanchuang.shengfubao.view.pinnedheader.PinnedHeaderAdapter;

import java.util.List;

public class ShopLinearAdapter extends PinnedHeaderAdapter<RecyclerView.ViewHolder> {

    private static final int VIEW_TYPE_ITEM_TIME = 0;
    private static final int VIEW_TYPE_ITEM_CONTENT = 1;



    private List<ShopBean> mDataList;
    private OnItemClickListener onItemClickListener;

    public ShopLinearAdapter() {
        this(null, null);
    }

    public ShopLinearAdapter(List<ShopBean> dataList, OnItemClickListener onTimeClickListener) {
        mDataList = dataList;
        this.onItemClickListener = onTimeClickListener;
    }

    public void setData(List<ShopBean> dataList) {
        mDataList = dataList;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == VIEW_TYPE_ITEM_TIME) {
            return new TitleHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_partner_title, parent, false));
        } else {
            return new ContentHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_linear_shop, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == VIEW_TYPE_ITEM_TIME) {
            TitleHolder titleHolder = (TitleHolder) holder;
            titleHolder.tvTitle.setText(mDataList.get(position).getTitle());
        } else {
            ContentHolder contentHolder = (ContentHolder) holder;
            contentHolder.textAdapterContentName.setText(mDataList.get(position).getContent().getName() + "\n" + mDataList.get(position).getContent().getDescribe());
            contentHolder.tvNumber.setText(mDataList.get(position).getContent().getNum());
            switch (mDataList.get(position).getContent().getStatus())
            {
                case "0":
                    contentHolder.ivStatue.setBackgroundResource(R.mipmap.icon_kao);
                    contentHolder.tvNumber.setTextColor(Color.parseColor("#FF676E7D"));
                    break;
                case "1":
                    contentHolder.ivStatue.setBackgroundResource(R.mipmap.icon_yi);
                    contentHolder.tvNumber.setTextColor(Color.parseColor("#FF676E7D"));
                    break;
                case "2":
                    contentHolder.ivStatue.setBackgroundResource(R.mipmap.icon_gou);
                    contentHolder.tvNumber.setTextColor(Color.parseColor("#FF676E7D"));
                    break;
                case "3":
                    contentHolder.ivStatue.setBackgroundResource(R.mipmap.icon_dai);
                    contentHolder.tvNumber.setTextColor(Color.parseColor("#FF676E7D"));
                    break;
            }
            contentHolder.rlRoot.setOnClickListener(v -> {
                onItemClickListener.onClick(v,position);
            });
        }
    }

    @Override
    public int getItemCount() {
        return mDataList == null ? 0 : mDataList.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (mDataList.get(position).getType() == 1) {
            return VIEW_TYPE_ITEM_TIME;
        } else {
            return VIEW_TYPE_ITEM_CONTENT;
        }
    }

    @Override
    public boolean isPinnedPosition(int position) {
        return getItemViewType(position) == VIEW_TYPE_ITEM_TIME;
    }

    static class ContentHolder extends RecyclerView.ViewHolder {
        TextView textAdapterContentName;
        TextView tvNumber;
        ImageView ivStatue;
        RelativeLayout rlRoot;

        ContentHolder(View itemView) {
            super(itemView);
            textAdapterContentName = itemView.findViewById(R.id.text_adapter_content_name);
            tvNumber = itemView.findViewById(R.id.tv_number);
            ivStatue = itemView.findViewById(R.id.iv_statue);
            rlRoot = itemView.findViewById(R.id.rl_root);

        }
    }

    static class TitleHolder extends RecyclerView.ViewHolder {

        TextView tvTitle;

        TitleHolder(View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.text_adapter_title_name);
        }
    }

    /**
     * 监听输入框内容
     */
    public interface OnItemClickListener {
        /**
         * Called when a view has been clicked.
         *
         * @param v The view that was clicked.
         */
        void onClick(View v, int position);
    }

}
